package com.maxima.attendance.actions;

import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.maxima.attendance.daos.ProjectDAO;
import com.maxima.attendance.daos.UserDAO;
import com.maxima.attendance.pojos.User;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport implements SessionAware {
	private static final long serialVersionUID = 1L;
	private User user = new User();
	private ProjectDAO projectDAO = new ProjectDAO();
	private Integer id;
	private Map<String, Object> map;
	// for logging
	static Logger logger = Logger.getLogger(ProjectAction.class.getName());

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Map<String, Object> getMap() {
		return map;
	}

	public void setMap(Map<String, Object> map) {
		this.map = map;
	}

	@Override
	public void setSession(Map<String, Object> map) {
		this.map = map;
	}

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	public String addNewUser() {
		this.user.setFirstName(ServletActionContext.getRequest().getParameter("firstname"));
		this.user.setLastName(ServletActionContext.getRequest().getParameter("lastname"));
		this.user.setEmployeeId(Integer.parseInt(ServletActionContext.getRequest().getParameter("employeeId")));
		this.user.setPassword(ServletActionContext.getRequest().getParameter("password"));
		this.user.setProject(projectDAO.get(Integer.parseInt(ServletActionContext.getRequest().getParameter("project"))));
		String status = ServletActionContext.getRequest().getParameter("getstatus");
		System.out.println("status is:" + status);
		Boolean userStatus = null;
		if (status.equals("active")) {
			userStatus = true;
		} else {
			userStatus = false;
		}
		this.user.setActive(userStatus);
		new UserDAO().add(this.user);
		logger.info("New Employee - " + this.user.getFirstName() + " Id - " + this.user.getId() + " - added Successfully");
		return SUCCESS;
	}

	public String selectUser() throws Exception {
		map.put(SessionManager.SELECTED_USER, new UserDAO().getUser(this.getId()));
		this.setSession(map);
		return SUCCESS;
	}

	public String updateUser() throws Exception {
		UserDAO userDAO = new UserDAO();
		int userId = ((User) this.getMap().get(SessionManager.SELECTED_USER)).getId();
		User user = userDAO.getUser(userId);
		user.setFirstName(ServletActionContext.getRequest().getParameter("firstname"));
		user.setLastName(ServletActionContext.getRequest().getParameter("lastname"));
		user.setEmployeeId(Integer.parseInt(ServletActionContext.getRequest().getParameter("employeeId")));
		System.out.println("Employee id is: " + user.getEmployeeId());
		user.setPassword(ServletActionContext.getRequest().getParameter("password"));
		user.setProject(projectDAO.get(Integer.parseInt(ServletActionContext.getRequest().getParameter("project"))));
		String status = ServletActionContext.getRequest().getParameter("getstatus");
		System.out.println("status is:" + status);
		Boolean userStatus = null;
		if (status.equals("active")) {
			userStatus = true;
		} else if (status.equals("inactive")) {
			userStatus = false;
		}
		user.setActive(userStatus);
		System.out.println("User status is:" + user.getActive());
		userDAO.update(userId, user);
		map.put(SessionManager.SELECTED_USER, null);
		this.setSession(map);
		logger.info("Employee - " + user.getFirstName() + " Id - " + userId + " - updated Successfully");
		return SUCCESS;
	}

	public String clearSelectedUser() {
		map.put(SessionManager.SELECTED_USER, null);
		return SUCCESS;
	}
}